﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;
using System.Windows.Forms;
using MediaPortal.Player;


namespace FlashUnity
{
  public class Player : Form
  {
    FlashUnityMessageFilter filter = new FlashUnityMessageFilter();
    public enum PlayerState { Init, Playing, Paused, Ended };
    public enum PlayerType { Auto, Unity3d, Flash };
    public enum PlayerWindow { Normal, FullScreen, Hidden, Disposed };
    public PlayerState playerState { get; private set; }
    public PlayerType playerType { get; set; }
    public Unity3d unity3dPlayer;
    public bool HasFocus { get; set; }
    public Flash flashPlayer;
    public event EventHandler OnExitCall;

    private bool _stretchToFit = true;
    private bool _windowlessVideo = true;
    private bool _enableContextMenu = false;
    private string _videoFile = string.Empty;
    private bool _allowFullScreen = true;
    private bool _autoSelectPlayer = true;
    private Point _videoWindowLocation;
    private Size _videoWindowSize;
    private PlayerWindow _windowState;

    public PlayerWindow PlayerWindowState
    {
      get { return _windowState; }
      set
      {
        _windowState = value;
        if (value == PlayerWindow.FullScreen)
          this.WindowState = FormWindowState.Maximized;
        else if (value == PlayerWindow.Normal)
          this.WindowState = FormWindowState.Normal;
      }
    }

    public bool WindowlessVideo
    {
      get { return _windowlessVideo; }
      set
      {
        _windowlessVideo = value;
        if (_windowlessVideo)
          this.FormBorderStyle = FormBorderStyle.None;
        else this.FormBorderStyle = FormBorderStyle.Sizable;
      }
    }

    public bool StretchToFit
    {
      get { return _stretchToFit; }
      set { _stretchToFit = value; }
    }

    public string VideoFile
    {
      get { return _videoFile; }
      set { _videoFile = value; }
    }

    public bool EnableContextMenu
    {
      get { return _enableContextMenu; }
      set { _enableContextMenu = value; }
    }

    public bool AutoSelectPlayer
    {
      get { return _autoSelectPlayer; }
      set { _autoSelectPlayer = value; }
    }

    public bool AllowFullScreen
    {
      get { return _allowFullScreen; }
      set { _allowFullScreen = value; }
    }

    public Size VideoWindowSize
    {
      get { return _videoWindowSize; }
      set
      {
        _videoWindowSize = value;
        this.ClientSize = _videoWindowSize;
      }
    }

    public Point VideoWindowLocation
    {
      get { return _videoWindowLocation; }
      set
      {
        _videoWindowLocation = value;
        this.Location = _videoWindowLocation;
      }
    }

    public void Play()
    {
      CreatePlayer();
    }

    public void Play(string videoFile)
    {
      _videoFile = videoFile;
      CreatePlayer();
    }

    public void Pause()
    {

    }

    public void Stop()
    {

    }

    protected override void OnClosing(System.ComponentModel.CancelEventArgs e)
    {
      Application.RemoveMessageFilter(filter);
      base.OnClosing(e);
    }

    internal void CreatePlayer()
    {
      if (string.IsNullOrEmpty(_videoFile))
      {

      }
      else if (_videoFile.Contains(".unity3d") && playerType == PlayerType.Auto || playerType == PlayerType.Unity3d)
      {
        unity3dPlayer = new Unity3d();
        unity3dPlayer.Play(_videoFile);
        unity3dPlayer.Dock = System.Windows.Forms.DockStyle.Fill;
        unity3dPlayer.GotFocus += Player_GotFocus;
        unity3dPlayer.LostFocus += Player_LostFocus;
        this.Controls.Add(unity3dPlayer);
        this.TopLevel = false;
        playerState = PlayerState.Playing;
      }
      else if (_videoFile.Contains(".swf") && playerType == PlayerType.Auto || playerType == PlayerType.Flash)
      {
        flashPlayer = new Flash();
        flashPlayer.Play(_videoFile);
        flashPlayer.Dock = System.Windows.Forms.DockStyle.Fill;
        flashPlayer.GotFocus += Player_GotFocus;
        flashPlayer.LostFocus += Player_LostFocus;
        this.Controls.Add(flashPlayer);
        this.TopLevel = false;
        playerState = PlayerState.Playing;
      }
    }

    void Player_LostFocus(object sender, EventArgs e)
    {
      HasFocus = false;
    }

    void Player_GotFocus(object sender, EventArgs e)
    {
      HasFocus = true;
    }

    public Player()
    {
      Application.AddMessageFilter(filter);
      filter.RightMouseDown += filter_RightMouseDown;
    }

    void filter_RightMouseDown(object sender, MouseEventArgs e)
    {
      if (this.OnExitCall != null) this.OnExitCall(this, new EventArgs());
    }
  }
}
